package com.lyc.common.command;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lyc.common.entity.BaseEntity;

/**
 * @Author 李大神仙
 * @Date 2024/3/19 21:31
 * @Version 1.0
 */
public class BaseCommand<M extends BaseMapper<T>,T extends BaseEntity<T>> extends SuperCommand<M,T> {
    /**
     * 新增实体
     * @param entity
     * @return
     */
    public Long insert(T entity){
        return this.insert(entity,e -> e.setId(null));
    }

    /**
     * 新增实体
     * @param entity
     * @param consumer
     * @return
     */
    public Long insert(T entity, Consumer<T> consumer){
        if (Objects.nonNull(consumer)){
            consumer.accept(entity);
        }
        return SqlHelper.retBool(baseMapper.insert(entity)) ? entity.getId() : null;
    }

    /**
     * 修改实体
     * @param entity
     * @return
     */
    public boolean update(T entity){
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    /**
     * 删除实体
     * @param ids
     * @return
     */
    public boolean delete(List<Long> ids){
        return SqlHelper.retBool(baseMapper.deleteBatchIds(ids));
    }


}
